package main

import (
	"fmt"
	"path/filepath"
	"strconv"
	"strings"
	"time"
)


const (
	TextBlack = iota + 30
	TextRed
	TextGreen
	TextYellow
	TextBlue
	TextMagenta
	TextCyan
	TextWhite
)

//将说明打印成蓝色
func debug(s string) {
	fmt.Printf("\x1b[0;%dm%s\x1b[0m\n", TextBlue, s)
}

func main() {
	debug("1, 声明变量字符串")
	var str = "hello world my name is wyang"
	fmt.Println(str)
	fmt.Printf("s=%s\n", str)

	debug("2, 字符串长度")
	fmt.Println(len(str))

	debug("3, 字符串切割")
	s := strings.Split(str, " ")
	fmt.Println(s)
	for _, v := range s {
		fmt.Println(v)
	}

	debug("4, 字符串替换")
	str1 := strings.Replace(str, " ", ",", -1)
	fmt.Println(str1)

	debug("4.1, 字符串替换:只替换第一个")
	str2 := strings.Replace(str, " ", ",", 1)
	fmt.Println(str2)
	debug("4.1, 字符串替换:替换前两个")
	str3 := strings.Replace(str, " ", ",", 2)
	fmt.Println(str3)

	debug("5, 子串查找")
	idx := strings.Index(str, " ")
	fmt.Println(idx)
	str4 := string([]byte(str)[idx+1:])
	fmt.Println(str4)

	debug("6, 比较大小")
	var tmp string = "0"
	str5 := []string{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "21"}
	for _,v := range str5 {
		if tmp < v {
			fmt.Printf("%s<%s\n", tmp, v)
		} else if tmp > v {
			fmt.Printf("%s>%s\n", tmp, v)
		}
		tmp = v
	}

	debug("7, 字符串比较")
	var str6 = "abc"
	var str7 = "abc"
	if str6 == str7 {
		fmt.Println("str6 == str7")
	} else {
		fmt.Println("str6 != str7")
	}


	debug("8 文件目录分离")
	var file = "/var/lib/ske/tls/server.crt"
	dir, filename := filepath.Split(file)
	fmt.Println(dir, filename)


    debug("9, int转string")
    a := 10
    fmt.Println("a=", strconv.FormatInt(int64(a), 10))

	time1 := fmt.Sprintf("now %x", time.Now().Unix())
	fmt.Println(time1)
}
